namespace Yaaf.WP7.NativeAccess.Registry
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    public class RegistryKey
    {
        private readonly string fullPath;

        public RegistryKey(string fullPath)
        {
            if (fullPath.EndsWith("\\"))
            {
                fullPath = fullPath.Substring(0, fullPath.Length - 1);
            }

            this.fullPath = fullPath;
        }

        public string FullPath
        {
            get
            {
                return fullPath;
            }
        }

        public string Name
        {
            get
            {
                return Path.GetFileName(fullPath);
            }
        }

        public IEnumerable<RegistryKey> Keys
        {
            get
            {
                return Registry.GetSubKeyNames(this.fullPath).Select(s => new RegistryKey(this.GetFullPath(s)));
            }
        }

        private string GetFullPath(string relative)
        {
            return string.IsNullOrEmpty(this.fullPath) ? relative : this.fullPath + "\\" + relative;
        }

        public IEnumerable<RegistryValue> Values
        {
            get
            {

                return Registry.GetValuesNames(this.fullPath).Select(s => new RegistryValue(GetFullPath(s)));
            }
        }

        public RegistryKey Parent
        {
            get
            {
                if (string.IsNullOrEmpty(this.fullPath))
                {
                    return null;
                }
                else
                {
                    var index = this.fullPath.LastIndexOf("\\");
                    if (index < 0)
                    {
                        return new RegistryKey("");
                    }
                    return new RegistryKey(this.fullPath.Substring(0, index));
                }
            }
        }

        public void Delete()
        {
            Registry.DeleteKey(this.fullPath);
        }

        public RegistryKey CreateSubKey(string name)
        {
            return new RegistryKey(this.fullPath + "\\" + name);
        }
    }

    public class RegistryValue
    {
        private readonly string fullPath;

        public string KeyPath
        {
            get
            {
                return fullPath.Substring(0, fullPath.LastIndexOf("\\"));
            }
        }

        public string FullPath
        {
            get
            {
                return fullPath;
            }
        }

        public string Name
        {
            get
            {
                return Path.GetFileName(fullPath);
            }
        }

        public RegistryValue(string fullPath)
        {
            this.fullPath = fullPath;
        }

        public object Value
        {
            get
            {
                return Registry.GetValue(this.fullPath);
            }
            set
            {
                Registry.SetValue(this.fullPath, value);
            }
        }

        public RegistryValueKind ValueType
        {
            get
            {
                return Registry.GetValueType(fullPath);
            }
        }
    }
}